package com.xuanwei.web.controller;

import com.xuanwei.core.entity.param.UserAddress;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwUserAddressService;
import com.xuanwei.dao.entity.XwUserAddress;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/11 14:12
 **/
@RestController
@Validated
@RequestMapping("wx/user/address")
public class UserAddressController extends BasicController {

    private IXwUserAddressService userAddressService;

    public UserAddressController(IXwUserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResultVO addAddress(@Valid UserAddress userAddress) {
        userAddressService.add(userAddress, getUserId());
        return ResultVO.successResult("添加成功");
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResultVO update(@Valid UserAddress userAddress) {
        userAddressService.update(userAddress);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO list() {
        List<UserAddress> userAddresses = userAddressService.findByUserId(getUserId());
        return ResultVO.successResult(userAddresses);
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public ResultVO delete(@Min(value = 1,message = "ID格式错误") Integer id) {
        userAddressService.delete(id);
        return ResultVO.successResult("删除成功");
    }

    @RequestMapping(value = "find", method = RequestMethod.GET)
    public ResultVO find(Integer id) {
        UserAddress userAddress = userAddressService.findById(id,getUserId());
        return ResultVO.successResult(userAddress);
    }

}
